<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/13
 * Time: 4:05 PM
 */

namespace api\models\search;


use api\data\ApiDataProvider;
use api\models\Job;
use api\models\Mission;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\Expression;

class MissionSearch extends Mission
{

    public $ad_code;
    public $job_type;
    public $salary;
    public $time; //工作时间


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'job_type',
                    'salary',
                    'time',
//                    'view_num'
                ],
                'integer'
            ],
            [
                [
                    'ad_code',
                ],
                'safe'
            ],
        ];
    }


    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Mission::find()->select("job.*");
        $query->joinWith('jobInfo');
        $query->joinWith('u');
        // add conditions that should always apply here


        $dataProvider = new ApiDataProvider([
            'query'      => $query,
            'sort'       => [
                'defaultOrder' => [
                    'created_at' => SORT_DESC,
//                    'sign_end'   => SORT_DESC
                ]
            ],
            'totalCount' => ''
        ]);

        $this->load($params, 'Search');
        if ( ! $this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
//        var_dump($query);

        switch ($this->ad_code) {
            case $this->ad_code % 10000 == 0:
                $query->addselect(['code' => new Expression("(`ad_code`div 10000)")]);
                $query->having(['code' => $this->ad_code / 10000]);
                break;
            case $this->ad_code % 100 == 0:
                $query->addselect(['code' => new Expression("(`ad_code`div 100)")]);
                $query->having(['ad_code' => $this->ad_code / 100]);
                break;
            default:
                $query->filterWhere(['ad_code' => $this->ad_code]);
                break;
        }


        // grid filtering conditions
        $query->andFilterWhere(['job_type' => $this->job_type])
              ->andFilterWhere(['>', 'salary', $this->salary])
              ->andFilterWhere([
                  'and',
                  ['<', 'start_at', $this->time],
                  ['>', 'end_at', $this->time]
              ]);

        $query->andwhere([
            'forbidden'  => Mission::ACCESS,
            'type'       => Mission::MISSION_TYPE,
            'hide'       => Job::ACCESS,
            'job.status' => Mission::PAY_STATUS
        ]);

//        var_dump($query);

        return $dataProvider;
    }


}